<template>
  <el-card style="width: 500px">
    <el-form :model="form" label-width="80px">


      <el-form-item label="充值余额" >
        <el-input v-model="form.account" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save" >确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>


<script>
import request from "@/util/request";

export default {
  name:"recharge",
  data(){
    return{
      form:{},
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  methods:{
    async getUser(){
      return  (await request.get("/user/userId/" + this.user.userId)).data;
    },

    async save(){
      await  request.post("/user/account/recharge",this.form).then(res=>{

        if(res.code=='200'){
          this.$message.success("充值成功！"),
              form.account=''

          //更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }else {
          this.$message.error("充值失败！")
        }
      })
    },

  }
}

</script>